/*	
	
	Copyright (c) 2007-2009 Ryan Christiansen
	
	This software is provided 'as-is', without any express or implied
	warranty. In no event will the authors be held liable for any damages
	arising from the use of this software.
	
	Permission is granted to anyone to use this software for any purpose,
	including commercial applications, and to alter it and redistribute it
	freely, subject to the following restrictions:
	
	1. The origin of this software must not be misrepresented; you must not
	claim that you wrote the original software. If you use this software
	in a product, an acknowledgment in the product documentation would be
	appreciated but is not required.
	
	2. Altered source versions must be plainly marked as such, and must not be
	misrepresented as being the original software.
	
	3. This notice may not be removed or altered from any source
	distribution.
	
*/
package funit.core
{
	import funit.framework.MessageString;
	
	import mx.collections.ArrayCollection;
	import mx.utils.StringUtil;
	
	import sv.utils.ClassUtil;
	import sv.utils.ErrorUtil;
	
	public class TestResult
	{
		private var _runState:RunState = RunState.NotRunnable;
		private var _resultState:ResultState = ResultState.Success;
		private var _failureSite:FailureSite = FailureSite.Unknown;
		private var _results:ArrayCollection;
		
		private var _name:String;
		private var _test:ITest;
		private var _description:String;
		
		private var _startTime:int;
		private var _time:int;
		
		private var _message:String;
		private var _stackTrace:String;
		
		
		/**
		 * 
		 * @param test
		 * @param name
		 */
		public function TestResult( test:ITest, name:String )
		{
			_name = name;
			_test = test;
			_runState = RunState.Runnable;
			
			if (test != null)
			{
				_description = test.description;
				_runState = test.runState;
				_message = test.ignoreReason;
			}
		}
		
		
		/**
		 * 
		 */
		public function success() : void
		{ 
			_runState = RunState.Executed;
			_resultState = ResultState.Success;
		}
		
		/**
		 * @param reason
		 */
		public function skip( reason:String ) : void
		{
			
		}
		
		/**
		 * @param reason
		 */
		public function ignore( reason:String ) : void
		{
			
		}
		
		/**
		 * @param runState
		 * @param reason
		 * @param stackTrace
		 */
		public function notRun( runState:RunState, reason:String, stackTrace:String ) : void
		{
			_runState = runState;
			_stackTrace = stackTrace;
		}
		
		/**
		 * @param message
		 * @param stackTrace
		 * @param failureSite
		 */
		public function failure( message:String, stackTrace:String, failureSite:FailureSite = null ) : void
		{
			_runState = RunState.Executed;
			_resultState = ResultState.Failure;
			_failureSite = (failureSite != null) ? failureSite : FailureSite.Test;
			_message = message;
			_stackTrace = stackTrace;
		}
		
		/**
		 * @param error
		 * @param failureSite
		 */
		public function error( error:Error, failureSite:FailureSite = null ) : void
		{
			_runState = RunState.Executed;
			_resultState = ResultState.Error;
			_failureSite = (failureSite != null) ? failureSite : FailureSite.Test;
			
			var errorMessage:String = buildErrorMessage(error);
			var errorStackTrace:String = buildErrorStackTrace(error);
			
			if (failureSite == FailureSite.TearDown)
			{
				errorMessage = "TearDown : " + errorMessage;
				errorStackTrace = "--TearDown" + MessageString.NewLine + errorStackTrace;
				
				if (_message != null)
				{
					errorMessage = _message + MessageString.NewLine + errorMessage;
				}
				
				if (_stackTrace != null)
				{
					errorStackTrace = _stackTrace + MessageString.NewLine + errorStackTrace;
				}
			}
			
			_message = errorMessage;
			_stackTrace = errorStackTrace;
		}
		
		/**
		 * @param error
		 * @return
		 */
		private function buildErrorMessage( error:Error ) : String
		{
			return	StringUtil.substitute(
						"{0} : {1}",
						ClassUtil.getQualifiedClassName(error),
						error.message
					);
		}
		
		/**
		 * @param error
		 * @return
		 */
		private function buildErrorStackTrace( error:Error ) : String
		{
			var stackTrace:String = ErrorUtil.getStackTrace(error);
			
			if (stackTrace)
			{
				return stackTrace;
			}
			else
			{
				return "No stack trace available";
			}
		}
		
		/**
		 * @param result
		 */
		public function addResult( result:TestResult ) : void
		{
			this.results.addItem(result);
			
			if ( this.resultState == ResultState.Success &&
				 result.resultState != ResultState.Success )
			{
				this.failure( "Child test failed.", null, FailureSite.Child );
			}
		}
		
		
		/**
		 * 
		 */
		public function get runState() : RunState
		{
			return _runState;
		}
		
		public function set runState( value:RunState ) : void
		{
			_runState = value;
		}
		
		
		/**
		 * 
		 */
		public function get resultState() : ResultState
		{
			return _resultState;
		}
		
		
		/**
		 * 
		 */
		public function get failureSite() : FailureSite
		{
			return _failureSite;
		}
		
		public function set failureSite( value:FailureSite ) : void
		{
			_failureSite = value;
		}
		
		
		/**
		 * 
		 */
		public function get executed() : Boolean
		{
			return (_runState == RunState.Executed);
		}
		
		
		/**
		 * 
		 */
		public function get isSuccess() : Boolean
		{
			return (_resultState == ResultState.Success);
		}
		
		
		/**
		 * 
		 */
		public function get isFailure() : Boolean
		{
			return (_resultState == ResultState.Failure || _resultState == ResultState.Error);
		}
		
		
		/**
		 * 
		 */
		public function get name() : String
		{
			return _name;
		}
		
		public function set name( value:String ) : void
		{
			_name = value;
		}
		
		
		/**
		 * 
		 */
		public function get test() : ITest
		{
			return _test;
		}
		
		
		/**
		 * 
		 */
		public function get description() : String
		{
			return _description;
		}
		
		public function set description( value:String ) : void
		{
			_description = value;
		}
		
		
		/**
		 * 
		 */
		public function get startTime() : int
		{
			return _startTime;
		}
		
		public function set startTime( value:int ) : void
		{
			_startTime = value;
		}
		
		
		/**
		 * 
		 */
		public function get time() : int
		{
			return _time;
		}
		
		public function set time( value:int ) : void
		{
			_time = value;
		}
		
		
		/**
		 * 
		 */
		public function get message() : String
		{
			return _message;
		}
		
		public function set message( value:String ) : void
		{
			_message = value;
		}
		
		
		/**
		 * 
		 */
		public function get stackTrace() : String
		{
			return _stackTrace;
		}
		
		public function set stackTrace( value:String ) : void
		{
			_stackTrace = value;
		}
		
		
		/**
		 * 
		 */
		public function get results() : ArrayCollection
		{
			if (_results == null)
			{
				_results = new ArrayCollection();
			}
			
			return _results;
		}
		
	}
	
}
